/**
 * Created with IntelliJ IDEA
 * Description:
 * User:恋恋
 * Date:2022-11-26
 * Time:20:35
 */
class Counter{
    int counter;
    //synchronized是可以加在这两个位置上的
//    synchronized public void add(){
//        counter++;
//    }
    public synchronized void add(){
        counter++;
    }
}
public class ThreadDemo1 {
    public static void main(String[] args) {
        Counter counter =new Counter();
        Thread thread0 = new Thread(()->{
            //for循环是可以并发执行的
            for (int i = 0; i < 5_0000; i++) {
                counter.add();
            }
        });
        Thread thread1 = new Thread(()->{
            for (int i = 0; i < 5_0000; i++) {
                counter.add();
            }
        });
        //开启线程
        thread0.start();;
        thread1.start();;
        //结束线程

        try {
            thread0.join();
            thread1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(counter.counter);
    }
}
